package com.li.drug.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.drug.entity.Supplier;
import com.li.drug.service.SupplierService;
import com.li.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 供应商相关controller
 */
@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    SupplierService supplierService;

    /**
     * 转向供应商页面
     * @return
     */
    @RequestMapping
    public String supplier(){
        return "supplier/supplier";
    }

    //分页查询供应商列表
    @RequestMapping("/supplierQueryPage")
    @ResponseBody
    public Object supplierQueryPage(String param,
                                    @RequestParam(name = "page") int pageNum,
                                    @RequestParam(name = "limit") int pageSize){
        try {
            Page<Supplier> page = supplierService.selectSupplierPage(pageNum,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(page);

        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向供应商新增页面
     * @return
     */
    @RequestMapping("/supplierPage")
    public String supplierPage(){
        return "supplier/supplierPage";
    }

    /**
     * 添加一个供应商
     */
    @RequestMapping("/supplierAdd")
    @ResponseBody
    public Object supplier(Supplier supplier){
        supplier.setCreatetime(new Date());
        int i = supplierService.addSupplier(supplier);
        return ResultMapUtil.getHashMapSave(i);
    }

    /**
     * 转向供应商编辑页面
     * @return
     */
    @RequestMapping("/supplierQueryById")
    public String supplierEditById(@RequestParam(name = "id") Integer id,
                                   Model model){
        Supplier supplier = supplierService.getSupplierById(id);
        model.addAttribute("supplier",supplier);
        return "supplier/supplierPage";
    }

    /**
     * 修改一个供应商
     */
    @RequestMapping("/supplierEdit")
    @ResponseBody
    public Object supplierEdit(Supplier supplier){
        try {
            int i = supplierService.editSupplier(supplier);
            return ResultMapUtil.getHashMapUpdate(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个供应商
     */
    @RequestMapping("/supplierDelById")
    @ResponseBody
    public Object supplierDelById(@RequestParam(name = "id") Integer id){
        try {
            int i = supplierService.delSupplierById(id);
            return ResultMapUtil.getHashMapDel(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有供应商列表
     */
    @RequestMapping("/supplierList")
    @ResponseBody
    public Object supplierList(){
        List<Supplier> list = supplierService.list();
        return list;
    }
}
